﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;
using TjZheng.Framework;

namespace TjZheng.CMS.Process
{
    /// <summary>
    /// 按钮工具栏
    /// </summary>
    public class ButtonTool
    {
        /// <summary>
        /// 按钮选项列表
        /// </summary>
        protected List<ButtonItem> Items { get; set; }
        /// <summary>
        /// 构造函数
        /// </summary>
        public ButtonTool()
        {
            this.Items = new List<ButtonItem>();
        }
        /// <summary>
        /// 添加按钮
        /// </summary>
        /// <param name="objButtonItem">按钮选项</param>
        public void AddItem(ButtonItem objButtonItem)
        {
            this.Items.Add(objButtonItem);
        }
        /// <summary>
        /// 批量添加按钮
        /// </summary>
        /// <param name="objButtonlist">按钮选项列表</param>
        public void AddItems(List<ButtonItem> objButtonlist)
        {
            this.Items.AddRange(objButtonlist);
        }
        /// <summary>
        /// 在指定位置添加按钮
        /// </summary>
        /// <param name="index">位置索引</param>
        /// <param name="objButtonItem">按钮选项</param>
        public void InsertItem(int index, ButtonItem objButtonItem)
        {
            this.Items.Insert(index, objButtonItem);
        }
        /// <summary>
        /// 在指定位置批量添加按钮
        /// </summary>
        /// <param name="index">位置索引</param>
        /// <param name="objButtonlist">按钮选项列表</param>
        public void InsertItems(int index, List<ButtonItem> objButtonlist)
        {
            this.Items.InsertRange(index, objButtonlist);
        }
        /// <summary>
        /// 创建按钮工具栏
        /// </summary>
        /// <returns></returns>
        public MvcHtmlString Create()
        {
            StringBuilder objStringBuilder = new StringBuilder();
            var objTopTool = this.Items.Where(c => c.Position == ButtonPosition.Top).ToList();
            if (objTopTool.IsNoNull())
            {
                objStringBuilder.AppendLine("<div class=\"top-tool\">");
                foreach (var objButtonItem in objTopTool)
                {
                    objStringBuilder.AppendLine("<a onclick=\"" + objButtonItem.Execution + ";\" class=\"easyui-linkbutton\" data-options=\"iconCls:'" + objButtonItem.ButtonIcon + "',plain:true\">" + objButtonItem.ButtonName + "</a>");
                }
                objStringBuilder.AppendLine("</div>");
            }
            var objRightTool = this.Items.Where(c => c.Position == ButtonPosition.Right).ToList();
            if (objRightTool.IsNoNull())
            {
                objStringBuilder.AppendLine("<div class=\"easyui-menu right-tool\">");
                foreach (var objButtonItem in objRightTool)
                {
                    objStringBuilder.AppendLine("<div onclick=\"" + objButtonItem.Execution + ";\" data-options=\"iconCls:'" + objButtonItem.ButtonIcon + "'\">" + objButtonItem.ButtonName + "</div>");
                }
                objStringBuilder.AppendLine("</div>");
            }
            var objOrdinaryTool = this.Items.Where(c => c.Position == ButtonPosition.Ordinary).ToList();
            if (objOrdinaryTool.IsNoNull())
            {
                foreach (var objButtonItem in objOrdinaryTool)
                {
                    objStringBuilder.AppendLine("<a onclick=\"" + objButtonItem.Execution + ";\" class=\"easyui-linkbutton\" data-options=\"iconCls:'" + objButtonItem.ButtonIcon + "',plain:true\">" + objButtonItem.ButtonName + "</a>");
                }
            }
            return MvcHtmlString.Create(objStringBuilder.ToString());
        }
    }
}
